﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace BabyBusMobileWeb.Core.Helper
{
   
    public class RestHelper
    {
        private HttpClient _baseClient;

        private HttpClient BaseClient
        {
			get { return _baseClient ?? (_baseClient = new HttpClient{BaseAddress = new Uri(Constants.BaseUrl)}); }
        }

        /// <summary>
        /// Query方法
        /// </summary>
        /// <typeparam name="T">query对象类型</typeparam>
        /// <param name="url">odata方式的url</param>
        /// <returns></returns>
        public async Task<ApiResult<T>> AsyncQuery<T>(string url)
        {
            try
            {
                var res = BaseClient.GetAsync(url).Result;
                var json = await res.Content.ReadAsStringAsync();

                if (string.IsNullOrEmpty(json))
                {
                    return new ApiResult<T>
                    {
                        Status = false,
                        ErrorMessage = "服务器没有返回。"
                    };
                }
                var result = JsonConvert.DeserializeObject<ApiResult<T>>(json);
                if (result == null)
                {
                    return new ApiResult<T>
                    {
                        Status = false,
                        ErrorMessage = "服务器返回的数据格式错误。"
                    };
                }

                return result;
            }
            catch (Exception e)
            {
                return new ApiResult<T>
                {
                    Status = false,
                    ErrorMessage = e.Message
                };
            }
        }

        /// <summary>
        /// Update的Get方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<ApiResponser> AsyncUpdate(string url)
        {
            try
            {
                var res = BaseClient.GetAsync(url).Result;
                var json = await res.Content.ReadAsStringAsync();

                if (string.IsNullOrEmpty(json))
                {
                    return new ApiResponser
                    {
                        Api_Status = false,
                        Message = "服务器没有返回。"
                    };
                }
                var result = JsonConvert.DeserializeObject<ApiResponser>(json);
                if (result == null)
                {
                    return new ApiResponser
                    {
                        Api_Status = false,
                        Message = "服务器返回的数据格式错误。"
                    };
                }

                return result;

                
            }
            catch(Exception e)
            {
                return new ApiResponser
                {
                    Api_Status = false,
                    Message  = e.Message
                };
            }
        }

        /// <summary>
        /// Update的Post方法
        /// </summary>
        /// <typeparam name="T">Attach对象类型</typeparam>
        /// <param name="url">url</param>
        /// <param name="postData">post对象</param>
        /// <returns></returns>
        public async Task<ApiResponser> AsyncUpdate(string url, object postData)
        {
            try
            {
                var postdata = JsonConvert.SerializeObject(postData);
                var content = new StringContent(postdata);
				content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var response = BaseClient.PostAsync(url, content).Result;
                var json = await response.Content.ReadAsStringAsync();

                if (string.IsNullOrEmpty(json))
                {
                    return new ApiResponser
                    {
                        Api_Status = false,
                        Message = "服务器没有返回。"
                    };
                }
                var result = JsonConvert.DeserializeObject<ApiResponser>(json);
                if (result == null)
                {
                    return new ApiResponser
                    {
                        Api_Status = false,
                        Message = "服务器返回的数据格式错误。"
                    };
                }
                return result;
            }
            catch (Exception e)
            {
                return new ApiResponser
                {
                    Api_Status = false,
                    Message = e.Message
                };
            }
        }
    }
}
